package com.boc.langxiang.ui.dialog;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.boc.langxiang.R;
import com.boc.langxiang.entity.bean.DesignerFilterBean;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Li Guoxi on 2016/12/22.
 */

public class DesignerFilterPop {

    private Context mContext;

    private OnDesignerFilterListener mOnDesignerFilterListener;

    private View rootView;
    private RecyclerView mRecyclerView;

    private BaseQuickAdapter<DesignerFilterBean> mAdapter;

    private PopupWindow mPopupWindow;

    public DesignerFilterPop(Context context, OnDesignerFilterListener onDesignerFilterListener) {
        this.mContext = context;
        this.mOnDesignerFilterListener = onDesignerFilterListener;
        init();
    }

    private void init() {
        rootView = LayoutInflater.from(mContext).inflate(R.layout.designer_filter_pop, null);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mAdapter = new BaseQuickAdapter<DesignerFilterBean>(R.layout.designer_filter_item, new ArrayList<DesignerFilterBean>()) {
            @Override
            protected void convert(BaseViewHolder helper, final DesignerFilterBean item) {
                helper.setText(R.id.title_tv, item.getTitle())
                        .getView(R.id.item_content).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnDesignerFilterListener.onSelect(item);
                        for (int i = 0; i < getData().size(); i++) {
                            getData().get(i).setSelected(false);
                        }
                        getData().get(getData().indexOf(item)).setSelected(true);
                        mPopupWindow.dismiss();
                        notifyDataSetChanged();
                    }
                });
                TextView textView = helper.getView(R.id.title_tv);
                if (item.isSelected()) {
                    textView.setTextColor(mContext.getResources().getColor(R.color.colorPrice));
                } else {
                    textView.setTextColor(mContext.getResources().getColor(R.color.black));
                }
            }
        };
        mRecyclerView.setAdapter(mAdapter);
        mPopupWindow = new PopupWindow(rootView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        initData();
    }

    private void initData() {
        String[] titles = new String[]{"全部", "产品设计师", "空间设计师"};
        List<DesignerFilterBean> list = new ArrayList<>();
        for (int i = 1; i <= titles.length; i++) {
            DesignerFilterBean bean = new DesignerFilterBean();
            bean.setId(i + "");
            bean.setTitle(titles[i - 1]);
            list.add(bean);
        }
        mAdapter.setNewData(list);
    }

    public void showPop(View view, PopupWindow.OnDismissListener onDismissListener) {
        mPopupWindow.setOnDismissListener(onDismissListener);
        mPopupWindow.showAsDropDown(view, 0, 3);
    }

    public void dismissPop() {
        mPopupWindow.dismiss();
    }

    public interface OnDesignerFilterListener {
        void onSelect(DesignerFilterBean filterBean);
    }
}
